home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime for java / tweencamera / src / tweencamera.java
Encoding:
Java Source  |  2000-09-28  |  5.3 KB  |  141 lines

  1. /*
  2.  * QuickTime for Java SDK Sample Code
  3.  
  4.    Usage subject to restrictions in SDK License Agreement
  5.  * Copyright: © 1996-1999 Apple Computer, Inc.
  6.  
  7.  */
  8. import java.io.*;
  9.  
  10. import quicktime.*;
  11. import quicktime.io.*;
  12. import quicktime.std.*;
  13. import quicktime.util.*;
  14. import quicktime.qd.*;
  15. import quicktime.qd3d.camera.*;
  16. import quicktime.qd3d.math.*;
  17. import quicktime.std.movies.*;
  18. import quicktime.std.movies.media.*;
  19. import quicktime.app.QTFactory;
  20.  
  21. public class TweenCamera implements StdQTConstants {
  22.     public static void main (String args[]) {
  23.         try {
  24.             System.out.println ("This will open the jet.mov and add a tween track to it, saving the result to the original movie");
  25.             QTSession.open();
  26.             makeMovie ();
  27.             System.out.println ("Finished");
  28.         }
  29.         catch (Exception e) {
  30.             e.printStackTrace();
  31.         } finally {
  32.             QTSession.close();
  33.         }
  34.     }
  35.         
  36.     private static void makeMovie () throws Exception {
  37.         // Get the 3d movie file from the user
  38.         QTFile movieFile = new QTFile (QTFactory.findAbsolutePath ("jet.mov"));
  39.         System.out.println (movieFile);
  40.         OpenMovieFile instream = OpenMovieFile.asRead(movieFile);
  41.         Movie myMovie = Movie.fromFile(instream);
  42.         instream.close();
  43.  
  44.         // get the 3d track and media from the movie
  45.         Track threeDTrack = myMovie.getIndTrack(1);
  46.         ThreeDMedia threeDMedia = (ThreeDMedia) Media.getTrackMedia(threeDTrack);
  47.  
  48.         // make a tween track and media
  49.         Track tweenTrack = myMovie.addTrack(0, 0, 0);
  50.         TweenMedia tweenMedia = new TweenMedia(tweenTrack, threeDMedia.getTimeScale());
  51.  
  52.         // make empty tween sample
  53.         AtomContainer tweenSample = new AtomContainer();
  54.         
  55.         // Construct shared data for the two camera descriptions
  56.         Point3D cameraTo = new Point3D (0.0F, 0.0F, 0.0F);
  57.         Vector3D cameraUp = new Vector3D (0.0F, 1.0F, 0.0F);
  58.         
  59.         float hither = 0.001F;
  60.         float yon = 1000.0F;
  61.         CameraRange range = new CameraRange(hither, yon);
  62.         
  63.         QDPoint origin = new QDPoint (-1.0F, 1.0F);
  64.         float width = 2.0F;
  65.         float height = 2.0F;
  66.         CameraViewPort cameraView = new CameraViewPort (origin, width, height);
  67.         
  68.  
  69. //**    addTweenEntryToSample ...    sets the type of the tween     
  70.             Point3D cameraFrom = new Point3D(0.0F, 0.0F, 30.0F);
  71.             CameraData tweenCamera = new CameraData(new CameraPlacement(cameraFrom, cameraTo, cameraUp), range, cameraView);
  72.  
  73.             // create entry for this tween in the sample
  74.             Atom tweenAtom = tweenSample.insertChild (new Atom(kParentAtomIsContainer), kTweenEntry, 1, 0);
  75.  
  76.             // define the type of this tween entry
  77.             tweenSample.insertChild(tweenAtom, kTweenType, 1, 0, EndianOrder.flipNativeToBigEndian32(kTweenType3dCameraData));
  78.             
  79.             //Endian flip tweenCamera in place
  80.             EndianOrder.flipNativeToBigEndian (tweenCamera, 0, CameraData.getEndianDescriptor());
  81.             
  82.             // define the 'flipped' data for this tween entry
  83.             tweenSample.insertChild(tweenAtom, kTweenData, 1, 0, tweenCamera);
  84.  
  85. //**    setTweenEntryInitialConditions
  86.         // Set camera initial conditions => the only difference is the initialLocation
  87.         // but because the camera data may have been flipped we create a clean copy
  88.         // we could have cloned this and then flipped it but this way we are explicit about dealing with 
  89.         // two different camera stuctures (even though they share values)
  90.         
  91.             // modify coordinates
  92.             Point3D initialLocation = new Point3D (0, 0, 150.0F);
  93.             CameraData initialCamera = new CameraData(new CameraPlacement(initialLocation, cameraTo, cameraUp), range, cameraView);
  94.  
  95.             // look up the tween entry
  96.             Atom initialTweenAtom = tweenSample.findChildByID_Atom (new Atom(kParentAtomIsContainer), kTweenEntry, 1);
  97.  
  98.             //Endian flip tweenCamera in place
  99.             EndianOrder.flipNativeToBigEndian (initialCamera, 0, CameraData.getEndianDescriptor());
  100.  
  101.             // add in the intial 'flipped' data
  102.             tweenSample.insertChild (initialTweenAtom, kTween3dInitialCondition, 1, 0, initialCamera); 
  103.  
  104. //**    addTweenEntryToInputMapEntry
  105.             // make up tween sample description
  106.             ThreeDDescription sampleDescription = new ThreeDDescription(0);
  107.             
  108.             // add the tween sample to the tween media
  109.             tweenMedia.beginEdits();
  110.             tweenMedia.addSample(tweenSample, 0, tweenSample.getSize(), threeDMedia.getDuration(),
  111.                                                      sampleDescription, 1, 0);
  112.             tweenMedia.endEdits();
  113.             
  114.             // add the tween media into the track
  115.             tweenTrack.insertMedia(0, 0, tweenMedia.getDuration(), 1);
  116.  
  117.             // create the reference between the 3d and tween tracks
  118.             int referenceIndex1 = threeDTrack.addReference(tweenTrack, kTrackModifierReference);
  119.             
  120.             // create input map for 3d track
  121.             AtomContainer inputMap = new AtomContainer();
  122.  
  123.             // add an input entry to the input map
  124.             Atom inputAtom = inputMap.insertChild (new Atom(kParentAtomIsContainer), kTrackModifierInput, referenceIndex1, 0);
  125.             
  126.             // set the type of the modifier input
  127.             inputMap.insertChild(inputAtom, kTrackModifierType, 1, 0, EndianOrder.flipNativeToBigEndian32(kTrackModifierCameraData));
  128.             
  129.             // set the sub input id (the id of the tween entry)
  130.             inputMap.insertChild(inputAtom, kInputMapSubInputID, 1, 0, EndianOrder.flipNativeToBigEndian32(1));
  131.  
  132. //** attach the input map to the 3d media handler
  133.             threeDMedia.setInputMap(inputMap);
  134.  
  135. //** save the newly created movie
  136.             OpenMovieFile outstream = OpenMovieFile.asWrite(movieFile);
  137.             myMovie.updateResource(outstream, movieInDataForkResID, movieFile.getName());
  138.             outstream.close();
  139.     }
  140. }
  141.